/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modulo4;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

import Modelo.DAL.Bono_DAO;

/**
 *
 * @author yngrdyn
 */
public class generarPlanillaBonosAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String TipoBono = request.getParameter("TipoBono");

        ActionErrors errors = new ActionErrors();
        
        if(TipoBono.equals("seleccionar")){
         errors.add("tipoBono_requerido", new ActionMessage("errors.tipoBono_requerido"));
         saveErrors(request, errors);
         return mapping.findForward(FAILURE);
        }

        String[] arrayTipoBono = TipoBono.split("/");
        
        String id_bono = arrayTipoBono[0];
        String tipo = arrayTipoBono[1];
        String sede = arrayTipoBono[2];

        System.out.println(id_bono);
        if(id_bono.equals("Alimentacion"))
        {
            System.out.println("Se asigna por dias, excepto los fines de semana");
        }
        else
        {
            if(id_bono.equals("Transporte"))
            {
                System.out.println("Se asigna por dias");
                Bono_DAO bonoTransporte = new Bono_DAO();
                boolean verif = bonoTransporte.generarBonoTransporte();
                if (!verif)
                {
                    errors.add("bono_noGenerado", new ActionMessage("bono_noGenerado"));
                    saveErrors(request, errors);
                    return mapping.findForward(FAILURE);
                }
            }
            else
            {
                if(id_bono.equals("Nocturno"))
                {
                    System.out.println("Se asigna por horas nocturnas trabajadas");
                }
                else
                {
                    System.out.println("Se asigna por horas");
                }
            }
        }

        errors.add("planillaBonosGenerada", new ActionMessage("errors.planillaBonosGenerada"));
        saveErrors(request, errors);
        return mapping.findForward(SUCCESS);
    }
}
